<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="项目编号" prop="projectNum">
      <el-select @change="getProductInfo()" :disabled="isEditMode"
                v-model="dataForm.projectNum"
                placeholder="请选择项目编号"
                class="label_len">
              <el-option
                  v-for="item in projectList"
                  :key="item.prProjectNumber"
                  :label="item.prProjectNumber + ' ' + item.prProjectName"
                  :value="item.prProjectNumber">
              </el-option>
            </el-select>
          </el-form-item>
    <el-form-item label="试验地点" prop="testLocation">
      <el-input v-model="dataForm.testLocation" placeholder="试验地点"></el-input>
    </el-form-item>
    <el-form-item label="样机编号" prop="prototypeNum">
      <el-input v-model="dataForm.prototypeNum" placeholder="样机编号"></el-input>
    </el-form-item>
    <el-form-item label="记录日期" prop="recordDate">
      <el-date-picker
        v-model="dataForm.recordDate"
        type="date"
        placeholder="选择记录日期">
      </el-date-picker>
    </el-form-item>
    
    <el-form-item label="开始时间" prop="startDate">
      <el-time-picker
        v-model="dataForm.startDate"
        placeholder="选择开始时间"
        @change="validateEndTime">
      </el-time-picker>
    </el-form-item>
    
    <el-form-item label="结束时间" prop="endDate">
      <el-time-picker
        v-model="dataForm.endDate"
        placeholder="选择结束时间"
        @change="validateEndTime">
      </el-time-picker>
      <div v-if="endTimeError" style="color: red;">结束时间必须大于开始时间</div>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="所用时间min" prop="timeUsed">
      <el-input v-model="dataForm.timeUsed" placeholder="所用时间min"></el-input>
    </el-form-item>
    <el-form-item label="作业面积hm2" prop="hm2">
      <el-input v-model="dataForm.hm2" placeholder="作业面积hm2"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          projectNum: '',
          testLocation: '',
          prototypeNum: '',
          recordDate: '',
          startDate: '',
          endDate: '',
          content: '',
          timeUsed: '',
          hm2: '',
          remark: '',
          inspectorName: '',
          recordName: '',
          checkerName: ''
        },
        projectList: [],
        isEditMode: false,
        endTimeError: false,
        dataRule: {
          projectNum: [
            { required: true, message: '项目编号不能为空', trigger: 'blur' }
          ],
          testLocation: [
            { required: true, message: '试验地点不能为空', trigger: 'blur' }
          ],
          prototypeNum: [
            { required: true, message: '样机编号不能为空', trigger: 'blur' }
          ],
          recordDate: [
            { required: true, message: '记录日期不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          timeUsed: [
            { required: true, message: '所用时间min不能为空', trigger: 'blur' }
          ],
          hm2: [
            { required: true, message: '作业面积hm2不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          inspectorName: [
            { required: true, message: '检测人姓名不能为空', trigger: 'blur' }
          ],
          recordName: [
            { required: true, message: '记录人姓名不能为空', trigger: 'blur' }
          ],
          checkerName: [
            { required: true, message: '校核人姓名不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted (){
    this.getprojectList()
  },
    methods: {
      getProductInfo(){
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getProductInfo/${this.dataForm.projectNum}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataForm.prototypeNum = data.productInfo.prProjectNumber
        }else {
          this.$message.error(data.msg)
        }
      })
    },
       //获取项目列表
      getprojectList() {
        this.$http({
          url: this.$http.adornUrl(`/reportapproval/product/getProjectNumber`),
          method: 'post',
          data: this.$http.adornData({
          })
        }).then((data) => {
          if (data && data.code === 0) {
            this.projectList = data.productList
          } else {
            this.$message.error(data.msg)
          }
        })
      },
       validateEndTime() {
        const { startDate, endDate } = this.dataForm;
        
        if (startDate && endDate) {
          
          this.endTimeError = startDate >= endDate; // 修改验证逻辑
        } else {
          this.endTimeError = false;
        }
      },
      init (id) {
         this.isEditMode = false;
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/reportapproval/productverificarecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.isEditMode =true;
                this.dataForm.projectNum=data.productVerificaRecord.projectNum
                this.dataForm.testLocation = data.productVerificaRecord.testLocation
                this.dataForm.prototypeNum = data.productVerificaRecord.prototypeNum
                this.dataForm.recordDate = data.productVerificaRecord.recordDate
                this.dataForm.startDate = data.productVerificaRecord.recordDate+" "+data.productVerificaRecord.startDate
                this.dataForm.endDate = data.productVerificaRecord.recordDate+" "+data.productVerificaRecord.endDate
                this.dataForm.content = data.productVerificaRecord.content
                this.dataForm.timeUsed = data.productVerificaRecord.timeUsed
                this.dataForm.hm2 = data.productVerificaRecord.hm2
                this.dataForm.remark = data.productVerificaRecord.remark
                this.dataForm.inspectorName = data.productVerificaRecord.inspectorName
                this.dataForm.recordName = data.productVerificaRecord.recordName
                this.dataForm.checkerName = data.productVerificaRecord.checkerName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/reportapproval/productverificarecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'projectNum': this.dataForm.projectNum,
                'testLocation': this.dataForm.testLocation,
                'prototypeNum': this.dataForm.prototypeNum,
                'recordDate': new Date(this.dataForm.recordDate).toISOString().split('T')[0],
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'content': this.dataForm.content,
                'timeUsed': this.dataForm.timeUsed,
                'hm2': this.dataForm.hm2,
                'remark': this.dataForm.remark,
                'inspectorName': this.dataForm.inspectorName,
                'recordName': this.dataForm.recordName,
                'checkerName': this.dataForm.checkerName
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
